<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/8/20
 * Time: 15:41
 */

namespace app\command\company\data_change;

use app\admin\model\AdminUser;
use app\admin\model\Company;
use app\admin\model\CompanyUser;
use app\company\model\CompanyDividend;
use app\company\model\PartnerDividend;
use app\company\model\Supplier;
use app\company\model\SupplierDividend;
use app\company\model\SupplierUser;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class DividendSupplier extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data:change:v.7.0')->setDescription('添加经销商分红比例信息');
    }


    protected function execute(Input $input, Output $output)
    {
        $output->writeln('添加经销商分红比例信息/companyOrderStorage添加字段active_time 初始化1111');
        $model = new Supplier();
        $model->startTrans();
        try {
            $model->select()->each(function ($item) {
                //已有公司添加admin  用户
                $user        = SupplierUser::supplierId($item->id)->find();
                $companyUser = CompanyUser::create([
                    'user_name'  => $user->user_name,
                    'company_id' => $user->company_id,
                    'password'   => md5(rand(10000, 99999) . rand(10000, 99999)),
                    'real_name'  => $user->real_name,
                    'mobile'     => $user->mobile,
                    'identity'   => 5
                ]);
                //添加分红设置
                SupplierDividend::create(['company_id' => $item->id, 'supplier_id' => $item->id, 'partner_id' => $companyUser->id]);
            });
            $model->commit();
            $output->writeln('添加经销商分红比例信息/companyOrderStorage添加字段active_time 初始化 成功');
        } catch (\Exception $e) {
            $model->rollback();
            $output->writeln('添加经销商分红比例信息/companyOrderStorage添加字段active_time 初始化失败' . $e->getMessage() . $e->getLine() . $e->getFile());
        }
    }
}